package com.zhang;

import java.util.concurrent.TimeUnit;

public class ReenterLockDemo {
    //synchronized和ReenterLock均为重用锁演示，其中synchronized是非公平锁，ReenterLock默认非公平锁
    public static void main(String[] args) {
        Phone phone = new Phone();
        new Thread(()->{
            try {
                phone.sendMS();
            }catch(Exception e){
                e.printStackTrace();
            }
        },"t1").start();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            try {
                phone.sendMS();
            }catch(Exception e){
                e.printStackTrace();
            }
        },"t2").start();
    }

    public static class Phone{
        public synchronized void sendMS(){
            System.out.println("当前线程名："+Thread.currentThread().getName()+"\t 发送短信！");
            sendEmail();
        }

        public synchronized void sendEmail(){
            System.out.println("当前线程名："+Thread.currentThread().getName()+"\t 发送邮件！");
        }
    }
}
